package com.jmx.yp.filter;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import com.jmx.yp.exception.ErrorCode;

/**
 * 
 * 功能描述：
 *         主要负责对验证码、session过期以及权限进行检查的过滤器
 * @author Jenny
 *
 * Date:2011-1-9 下午11:26:42
 */
public class CheckFilter implements Filter{
	public void init(FilterConfig config) throws ServletException {}

	public void destroy() { }

	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
	    	throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;	
		
		System.out.println(request.getContextPath());
		
		String code = request.getParameter("validNum");
		String securityCode = (String)request.getSession().getAttribute("securityCode");		   
		request.getSession().removeAttribute("securityCode");
		
		if (code.equalsIgnoreCase(securityCode)) {
			filterChain.doFilter(request, response);
		}else{			   			 
			request.setAttribute("LOGIN_ERROR", ErrorCode.VALIDNUM_ERROR);
 		    request.getRequestDispatcher("loginInit.action").forward(request, response);			   							  
	    }
	}
}
